module bmp_test;

// typedef struct tagBITMAPINFOHEADER {
//     DWORD biSize;
//     LONG biWidth;
//     LONG biHeight;
//     WORD biPlanes;
//     WORD biBitCount;
//     DWORD biCompression;
//     DWORD biSizeImage;
//     LONG biXPelsPerMeter;
//     LONG biYPelsPerMeter;
//     DWORD biClrUsed;
//     DWORD biClrImportant;
// } BITMAPINFOHEADER,*LPBITMAPINFOHEADER,*PBITMAPINFOHEADER;

`define IMAGE_WIDTH 16
`define IMAGE_HEIGHT 16

integer img_file;
integer i, j;

wire signed[31:0] img_x = `IMAGE_WIDTH;
wire signed[31:0] img_y = `IMAGE_HEIGHT;

wire[31:0] img_size = img_x * img_y * 3;

// BGR格式，Windows的Bitmap是BGR
reg[23:0] frame[`IMAGE_WIDTH-1:0][`IMAGE_HEIGHT-1:0];

initial begin
    img_file = $fopen("out.bmp", "wb");
    $fwrite(img_file, "BM%u%u%u", 14, 0, 54);
    $fwrite(img_file, "%u%u%u%u%u%u%u%u%u%u",
        40, img_x, img_y, {16'd24, 16'd1}, 0, img_size, 0, 0, 0, 0);
    for(i = 0; i < `IMAGE_WIDTH; i++) begin
        for(j = 0; j < `IMAGE_HEIGHT; j++) begin
            frame[i][j] = 24'hAAFF30;
        end
    end
    // 写入帧缓存
    for(i = 0; i < `IMAGE_WIDTH; i++) begin
        for(j = 0; j < `IMAGE_HEIGHT; j++) begin
            $fwrite(img_file, "%c%c%c", frame[i][j][7:0], frame[i][j][15:8], frame[i][j][23:16]);
        end
    end
    $fclose(img_file);
end
    
endmodule